package com.xm.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.xm.context.BaseContext;
import com.xm.domain.dto.ShopDto;
import com.xm.domain.po.Employee;
import com.xm.domain.po.Shop;
import com.xm.domain.po.ShopAddressBook;
import com.xm.mapper.ShopMapper;
import com.xm.service.ShopService;
import com.xm.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Override
    @Transactional
    public void saveShop(ShopDto shopDto) {

        //1 插入店铺数据

        Shop shop = BeanUtils.copyBean(shopDto, Shop.class);
        //2 默认商家创建下线状态
        shop.setStatus(2);
        shop.setCloseTime(LocalDateTime.now());
        shop.setUpdatedTime(LocalDateTime.now());
        save(shop);
        //2 将店铺 id 存入 商家
        Long currentId = BaseContext.getCurrentId();
        Db.lambdaUpdate(Employee.class)
                .eq(Employee::getId,currentId)
                .set(Employee::getShopId,shop.getId())
                .update();

        //3 存储商家地址地址
        ShopAddressBook shopAddressBook = shopDto.getShopAddressBook();
        shopAddressBook.setShopId(shop.getId());
        Db.save(shopAddressBook);

    }
}
